﻿#include "bgmcontrol.h"
#include "playhand.h"

#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRandomGenerator>
#include <QTimer>
#include <QDebug>
BGMControl::BGMControl(QObject *parent) : QObject(parent)
{
    for(int i=0;i<5;i++)
    {
        QMediaPlayer* player=new QMediaPlayer(this);
        QMediaPlaylist* list=new QMediaPlaylist(this);
        if(i<2 || i==4)
        {
            list->setPlaybackMode(QMediaPlaylist::CurrentItemOnce);
        }
        else if(i==2)
        {
            list->setPlaybackMode(QMediaPlaylist::Loop);
        }
        player->setPlaylist(list);
        player->setVolume(100);
        m_players.push_back(player);
        m_lists.push_back(list);
    }
    initPlayList();
}

void BGMControl::initPlayList()
{
    //存储json的key值
    QStringList list;
    list<<"Man"<<"WoMan"<<"BGM"<<"Other"<<"Ending";

    QFile file(":/conf/playList.json");
    file.open(QFile::ReadOnly);
    QByteArray json=file.readAll();
    file.close();
    //转换为文档对象
    QJsonDocument doc=QJsonDocument::fromJson(json);
    //文档对象转换为json对象
    QJsonObject obj=doc.object();
    for(int i=0;i<list.size();i++)
    {
        QString prefix=list.at(i);
        QJsonArray array= obj.value(prefix).toArray();
        for(int j=0;j<array.size();j++)
        {
            m_lists[i]->addMedia(QMediaContent(QUrl(array[j].toString())));
        }
    }


}

void BGMControl::startBGM(int volume)
{
    //设置播放的音乐序号
    m_lists[2]->setCurrentIndex(0);
    //设定音乐声音大小
    m_players[2]->setVolume(volume);
    //开始播放
    m_players[2]->play();
}

void BGMControl::stopBGM()
{
    m_players[2]->stop();
}

void BGMControl::playerRobLordMusic(int point, BGMControl::RoleSex sex, bool isFirst)
{
    int index=sex==man?0:1;
    if(isFirst && point>0)
    {
        qDebug()<<"1";
        m_lists[index]->setCurrentIndex(Order);
    }
    else if(point==0)
    {
        if(isFirst)
        {
            qDebug()<<"2";
            m_lists[index]->setCurrentIndex(NoOrder);
        }
        else
        {
            qDebug()<<"3";
            m_lists[index]->setCurrentIndex(NoRob);
        }
    }
    else if(point==2)
    {
        qDebug()<<"4";
        m_lists[index]->setCurrentIndex(Rob1);
    }
    else if(point==3)
    {
        qDebug()<<"5";
        m_lists[index]->setCurrentIndex(Rob2);
    }
    m_players[index]->play();
    qDebug()<<"qiangdizhu1";
}

void BGMControl::playCardMusic(Cards cards, bool isFirst, BGMControl::RoleSex sex)
{
    // 得到播放列表
        int index = sex == man ? 0 : 1;
        QMediaPlaylist* list = m_lists[index];

        Card::CardPoint pt = Card::CardPoint::Card_Begin;
        // 取出牌型 然后进行判断
        PlayHand hand(cards);
        PlayHand::HandType type = hand.getHandType();
        if(type == PlayHand::Hand_Single || type == PlayHand::Hand_Pair || type == PlayHand::Hand_Triple)
        {
            pt = cards.takeRandCard().point();
        }
        int number = 0;
        switch(type)
        {
        // 单牌
        case PlayHand::Hand_Single:
            number = pt - 1;
            break;
        // 对牌
        case PlayHand::Hand_Pair:
            number = pt - 1 + 15;
            break;
        // 三张点数相同的牌
        case PlayHand::Hand_Triple:
            number = pt - 1 + 15 + 13;
            break;
        // 三带一
        case PlayHand::Hand_Triple_Single:
            number = ThreeBindOne;
            break;
        // 三带二
        case PlayHand::Hand_Triple_Pair:
            number = ThreeBindPair;
            break;
        // 飞机
        case PlayHand::Hand_Plane:
        // 飞机带两个单
        case PlayHand::Hand_Plane_Two_Single:
        // 飞机带两个对儿
        case PlayHand::Hand_Plane_Two_Pair:
            number = Plane;
            break;
        // 连对
        case PlayHand::Hand_Seq_Pair:
            number = SequencePair;
            break;
        // 顺子
        case PlayHand::Hand_Seq_Single:
            number = Sequence;
            break;
        // 炸弹
        case PlayHand::Hand_Bomb:
            number = Bomb;
            break;
        // 王炸
        case PlayHand::Hand_Bomb_Jokers:
            number = JokerBomb;
            break;
        // 炸弹带一对儿
        case PlayHand::Hand_Bomb_Pair:
        // 炸弹带两单
        case PlayHand::Hand_Bomb_Two_Single:
        // 王炸带一对儿
        case PlayHand::Hand_Bomb_Jokers_Pair:
        // 王炸带两单
        case PlayHand::Hand_Bomb_Jokers_Two_Single:
            number = FourBindTwo;

        default:
            break;
        }

        if(!isFirst && (number >= Plane && number <= FourBindTwo))
        {
            list->setCurrentIndex(MoreBiger1 + QRandomGenerator::global()->bounded(2));
        }
        else
        {
            list->setCurrentIndex(number);
        }
        // 播放音乐
        m_players[index]->play();
        if(number == Bomb || number == JokerBomb)
        {
            playAssistMusic(BombVoice);
        }
        if(number == Plane)
        {
            playAssistMusic(PlaneVoice);
        }
        qDebug()<<"音乐序号:"<<QString::number(number);
}

void BGMControl::playLastMusic(BGMControl::CardType type, BGMControl::RoleSex sex)
{
    int index = sex == man ? 0 : 1;
    QMediaPlaylist* list = m_lists[index];

    if(m_players[index]->state()==QMediaPlayer::StoppedState)
    {
        m_lists[index]->setCurrentIndex(type);
        m_players[index]->play();
    }
    else
    {
        QTimer::singleShot(1000,this,[=](){
            m_lists[index]->setCurrentIndex(type);
            m_players[index]->play();
        });
    }

}

void BGMControl::playPassMusic(BGMControl::RoleSex sex)
{
    // 1. 玩家的性别
        int index = sex == man ? 0 : 1;
        // 2. 找到播放列表
        QMediaPlaylist* list = m_lists[index];
        // 3. 找到要播放的音乐
        int random = QRandomGenerator::global()->bounded(4);
        list->setCurrentIndex(Pass1 + random);
        // 4. 播放音乐
        m_players[index]->play();

}

void BGMControl::playEndingMusic(bool isWin)
{
    if(isWin)
    {
        m_lists[4]->setCurrentIndex(0);
    }
    else
    {
        m_lists[4]->setCurrentIndex(1);
    }
    m_players[4]->play();
}

void BGMControl::playAssistMusic(BGMControl::AssistMusic type)
{
    QMediaPlaylist::PlaybackMode mode;
    if(type == Dispatch)
    {
        // 循环播放
        mode = QMediaPlaylist::CurrentItemInLoop;
    }
    else
    {
        // 单曲播放一次
        mode = QMediaPlaylist::CurrentItemOnce;
    }
    // 2. 找到播放列表
    QMediaPlaylist* list = m_lists[3];
    // 3. 找到要播放的音乐
    list->setCurrentIndex(type);
    list->setPlaybackMode(mode);
    // 4. 播放音乐
    m_players[3]->play();

}

void BGMControl::stopAssisMusic()
{

    m_players[3]->stop();
}
